﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

public partial class CartaPorteListado : PageBase
{
    #region StrAcId
    public string StrAcId
    {
        get
        {
            if (ViewState["StrAcId"] != null)
                return ViewState["StrAcId"].ToString();
            return "";
        }
        set { ViewState["StrAcId"] = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        Server.Transfer("CartaPorte.aspx?SSM_ID=53", true);
    }

    #region crearContextItems
    private void crearContextItems()
    {
        try
        {
            Context.Items["StrAcId"] = StrAcId;
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
        }
    }
    #endregion

    protected void btnActualizar_Click(object sender, EventArgs e)
    {
        List<object> keyValues = ASPxGridViewOrdenSalida.GetSelectedFieldValues("AC_ID");
        Model.DataClassesAutosolDataContext contexto = new Model.DataClassesAutosolDataContext(ConfigurationManager.AppSettings["Conn"]);
        try
        {
            foreach (object key in keyValues)
            {
                StrAcId = key.ToString();
            }
            crearContextItems();
            Server.Transfer("CartaPorte.aspx?SSM_ID=53", true);
            //Response.Redirect("EntidadesAdministracion.aspx", true);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
        }
    }
    protected void btnEliminar_Click(object sender, EventArgs e)
    {
        List<object> keyValues = ASPxGridViewOrdenSalida.GetSelectedFieldValues("AC_ID");
        try
        {
            foreach (object key in keyValues)
            {
                Model.DataClassesAutosolDataContext contexto = new Model.DataClassesAutosolDataContext(ConfigurationManager.AppSettings["Conn"]);


                //liberar los vives asociados
                var datosVinesAsociados = from b in contexto.AUT_CARTAs
                                          where b.AC_ID == Convert.ToDecimal(key.ToString())
                                          select b;
                foreach (var v in datosVinesAsociados)
                {
                    contexto.AUT_CARTAs.DeleteOnSubmit(v);
                    contexto.SubmitChanges();
                }


                ////elimna la orden
                //var datos = from a in contexto.AUT_ORDEN_SALIDAs
                //            where a.AOS_ID == Convert.ToDecimal(key.ToString())
                //            select a;
                //foreach (var v in datos)
                //{
                //    contexto.AUT_ORDEN_SALIDAs.DeleteOnSubmit(v);
                //    contexto.SubmitChanges();
                //}
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('Se eliminaron los registros Exitosamente');", true);
               // bindDatosOrdenSalida();
            }

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
        }
    }
    protected void btnRemesa_Click(object sender, EventArgs e)
    {
        List<object> keyValues = ASPxGridViewOrdenSalida.GetSelectedFieldValues("AC_ID");
        //Model.DataClassesAutosolDataContext contexto = new Model.DataClassesAutosolDataContext(ConfigurationManager.AppSettings["Conn"])
        try
        {
            foreach (object key in keyValues)
            {
                //Session["AOS_ID"] = lblNumeroOrdenCargue.Text;
                Session["AC_ID"] = key.ToString();
                //crearContextItems();
                System.Text.StringBuilder l_msg = new System.Text.StringBuilder();
                //TODO: Revisar esta impresión.
                l_msg.Append(@"<script language=javascript>");
                l_msg.Append(@"window.open('CartaPorteReporte.aspx" + @"', 'VentanaImpresion','location=0, status=0, directories=0, width=800, height=600, resizable=no, hotkeys=no, scrollbars=yes, toolbar=no, menubar=no, personalbar=no');</script>");
                Response.Write(l_msg.ToString());
            }

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
        }
    }
    protected void ASPxButtonDetai_Click(object sender, EventArgs e)
    {
        List<object> keyValues = ASPxGridViewOrdenSalida.GetSelectedFieldValues("AC_ID");
        //Model.DataClassesAutosolDataContext contexto = new Model.DataClassesAutosolDataContext(ConfigurationManager.AppSettings["Conn"])
        try
        {
            foreach (object key in keyValues)
            {
                //Session["AOS_ID"] = lblNumeroOrdenCargue.Text;
                Session["AC_ID"] = key.ToString();
                //crearContextItems();
                System.Text.StringBuilder l_msg = new System.Text.StringBuilder();
                //TODO: Revisar esta impresión.
                l_msg.Append(@"<script language=javascript>");
                l_msg.Append(@"window.open('DTAIReporte.aspx" + @"', 'VentanaImpresion','location=0, status=0, directories=0, width=800, height=600, resizable=no, hotkeys=no, scrollbars=yes, toolbar=no, menubar=no, personalbar=no');</script>");
                Response.Write(l_msg.ToString());
            }

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
        }
    }
}